﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myAC.DataProviders
{
    public class SupportTopic
    {
        public Int32 MaxTopicDepthExtended = 5;  // currently it seems only 3 levels are used.

        String supportTopicFullpah = String.Empty;
        public String SupportTopicFullpath 
        {
            get
            {
                return supportTopicFullpah;
            }
            set
            {
                supportTopicFullpah = value;
                CallCodeNameAtDifferentLevels = SupportTopicFullpath.Split('\\').ToList();
                RealMaxTopicDepthWithoutExtending = CallCodeNameAtDifferentLevels.Count();
                ExtendLastTopicNameToAllRemainingLevelsAsNecessary();
            }
        }

        public String SupportTopicDerivedId { get; set; } // this is a unique integer, can be used as label.

        String callCodeID = String.Empty;
        public String CallCodeID  // this is unique per topic
        {
            //CallCodeID looks like this: 1000501\111371
            get { return callCodeID; }
            set
            {
                callCodeID = value;
                CallCodeIdAtDifferentLevels = CallCodeID.Split('\\').ToList();
                RealMaxTopicDepthWithoutExtending = CallCodeIdAtDifferentLevels.Count();
                ExtendLastTopicIDToAllRemainingLevelsAsNecessary();
            }
        }

        public Boolean IsActive { get; set; }

        public List<String> CallCodeIdAtDifferentLevels { get; set; }  // currently, there're at most 15 levels. index = level-1.

        public List<String> CallCodeNameAtDifferentLevels { get; set; }

        public Int32 RealMaxTopicDepthWithoutExtending { get; set; }


        /// <summary>
        /// Before:
        /// SR1: L1\L2
        /// SR2: L1\L2\L3\L4
        /// 
        /// After:
        /// SR1: L1\L2\L2\L2  (extended)
        /// SR2: L1\L2\L3\L4
        /// </summary>
        private void ExtendLastTopicIDToAllRemainingLevelsAsNecessary()
        {
            String lastCallCodeID = CallCodeIdAtDifferentLevels[RealMaxTopicDepthWithoutExtending - 1]; // this is what to extend. L2
            for (Int32 i = RealMaxTopicDepthWithoutExtending; i < MaxTopicDepthExtended; i++)
            {
                CallCodeIdAtDifferentLevels.Add(lastCallCodeID);
            }
        }

        private void ExtendLastTopicNameToAllRemainingLevelsAsNecessary()
        {
            String lastCallCodeName = CallCodeNameAtDifferentLevels[RealMaxTopicDepthWithoutExtending - 1]; // this is what to extend.
            for (Int32 i = RealMaxTopicDepthWithoutExtending; i < MaxTopicDepthExtended; i++)
            {
                CallCodeNameAtDifferentLevels.Add(lastCallCodeName);
            }

        }
        


        public override string ToString()
        {
#if DEBUG
            return SupportTopicFullpath;
#else 
            return callCodeID;
#endif
        }
    }
}
